package com.shiku.im.security.controller;

import com.shiku.im.security.entity.ResourceInfo;
import com.shiku.im.security.entity.ResultDataSelect;
import com.shiku.im.security.entity.ResultDataSelect2;
import com.shiku.im.security.entity.SecurityRole;
import com.shiku.im.security.service.ResourceInfoManager;
import com.shiku.im.security.service.SecurityRoleManager;
import com.shiku.im.user.entity.Role;
import com.shiku.im.user.service.impl.RoleManagerImpl;
import com.shiku.im.vo.JSONMessage;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO (资源接口操作)
 * @Author xie yuan yang
 * @Date 2020/3/6
 **/
@ApiIgnore
@RestController
@RequestMapping("/console")
public class ResourceInfoController {

    @Autowired
    private ResourceInfoManager resourceInfoManager;

    @Autowired
    private SecurityRoleManager securityRoleManager;

    @Autowired
    private RoleManagerImpl roleManager;

    /**
     * @Description //TODO 查询全部资源
     * @Date 9:29 2020/3/7
     **/
    @RequestMapping(value = "/queryResourceInfo")
    public JSONMessage queryResourceInfo(){
        List<ResourceInfo> data = resourceInfoManager.queryResourceInfo();
        return JSONMessage.success(data);
    }

    /**
     * @Description //TODO  删除资源
     * @Date 9:29 2020/3/7
     **/
    @RequestMapping(value = "/delResourceInfo")
    public JSONMessage delResourceInfo(String id){
        List<ResourceInfo> resourceInfos = resourceInfoManager.queryResourceInfoByPid(id);
        ObjectId objectId = new ObjectId();
        if (resourceInfos.size() > 0){
            if (resourceInfos.size() == 1){//表示可能有一个 展示数据
                for (ResourceInfo resourceInfo : resourceInfos) {
                    if (resourceInfo.getIsView() == 0){
                        return JSONMessage.failure("该资源下有数据不能删除！");
                    }else{
                        objectId = resourceInfo.getId();
                    }
                }
            }else{
                return JSONMessage.failure("该资源下有数据不能删除！");
            }
        }

        ResourceInfo resourceInfo = resourceInfoManager.queryResourceInfoById(new ObjectId(id));
        //维护角色权限数据
        List<SecurityRole> securityRoles = securityRoleManager.querySecurityRol1e();
        for (SecurityRole securityRole : securityRoles) {
            if (securityRole.getRoleResourceList() != null){
                //遍历所有角色
                boolean flag = false;
                List<String> roleResourceList = securityRole.getRoleResourceList();
                for (int i = 0; i < roleResourceList.size(); i++) {
                    if (roleResourceList.get(i).equals(String.valueOf(resourceInfo.getId()))){
                        roleResourceList.remove(i);
                        flag = true;
                    }
                }

                if (flag){
                    securityRoleManager.updateRoleResource(securityRole.getRoleId(),roleResourceList);
                    flag = false;
                }
            }
        }

        //维护用户权限资源数据
        List<Role> roles = roleManager.queryAllRole();
        for (Role role : roles) {
            boolean flag = false;
            //查询全部用户权限
            List<String> resourceIdList = role.getResourceIdList();
            if (resourceIdList != null){
                for (int i = 0; i < resourceIdList.size(); i++) {
                    if (String.valueOf(resourceIdList.get(i)).equals(String.valueOf(resourceInfo.getId()))){
                        resourceIdList.remove(i);
                        flag = true;
                    }
                }

                if (flag){
                    roleManager.updateUserRole(role.getUserId(),resourceIdList);
                    flag = false;
                }
            }
        }
        resourceInfoManager.delResourceInfo(objectId);
        resourceInfoManager.delResourceInfo(new ObjectId(id));
        return JSONMessage.success();
    }

    /**
     * @Description //TODO  修改资源
     * @Date 9:29 2020/3/7
     **/
    @RequestMapping(value = "/updateResourceInfo")
    public JSONMessage updateResourceInfo(ResourceInfo resourceInfo,String resourceId){
        resourceInfo.setId(new ObjectId(resourceId));
        resourceInfoManager.updateResourceInfo(resourceInfo);
        return JSONMessage.success();
    }

    /**
     * @Description //TODO  添加资源
     * @Date 9:30 2020/3/7
     **/
    @RequestMapping(value = "/addResourceInfo")
    public JSONMessage sava(ResourceInfo resourceInfo){
        if (resourceInfo.getResourceAuth().equals("")){
            return JSONMessage.failure("元素权限不能为空！");
        }
        List<ResourceInfo> resourceInfos = resourceInfoManager.queryResourceInfoByResourceAuth(resourceInfo.getResourceAuth());
        if (resourceInfos.size() > 0){
           return JSONMessage.failure("元素权限已存在！");
        }
        /*List<ResourceInfo> resourceInfos1 = resourceInfoManager.queryResourceInfoByResourceName(resourceInfo.getResourceName());
        if (resourceInfos1.size() > 0){
            return  JSONMessage.failure("资源名称已存在！");
        }*/
/*        List<ResourceInfo> resourceInfos2 = resourceInfoManager.queryResourceInfoByResourceUrl(resourceInfo.getResourceUrl());
        if (resourceInfos2.size() > 0){
            return JSONMessage.failure("资源路径已存在！");
        }*/

        //判断是否是菜单
        ResourceInfo sava = resourceInfoManager.sava(resourceInfo);
        if (sava.getType().equals("1")){
            //设置只看权限————解决UI联动问题而加入的
            ResourceInfo resourceInfo1 = new ResourceInfo();
            resourceInfo1.setType("1");
            resourceInfo1.setPid(String.valueOf(sava.getId()));
            resourceInfo1.setStatus((byte) 1);
            resourceInfo1.setIsView(1);
            resourceInfo1.setResourceUrl("");
            resourceInfo1.setResourceName("展示数据");
            resourceInfo1.setResourceUrl("");
            resourceInfoManager.sava(resourceInfo1);
        }

        return JSONMessage.success();
    }

    @RequestMapping(value = "/qq")
    public JSONMessage sava2(String pid){

        //设置只看权限————解决UI联动问题而加入的
        ResourceInfo resourceInfo1 = new ResourceInfo();
        resourceInfo1.setType("1");
        resourceInfo1.setPid(String.valueOf(pid));
        resourceInfo1.setStatus((byte) 1);
        resourceInfo1.setIsView(1);
        resourceInfo1.setResourceUrl("");
        resourceInfo1.setResourceName("展示数据");
        resourceInfo1.setResourceUrl("");
        resourceInfoManager.sava(resourceInfo1);
        return JSONMessage.success();
    }

    /**
     * @Description //TODO 查询资源根据id
     * @Date 9:30 2020/3/7
     **/
    @RequestMapping(value = "/queryResourceInfoById")
    public JSONMessage sava(String id){
        ResourceInfo data = resourceInfoManager.queryResourceInfoById(new ObjectId(id));
        return JSONMessage.success(data);
    }

    /**
     * @Description //TODO 查询资源————多选下拉菜单
     * @Date 9:30 2020/3/7
     **/
    @RequestMapping(value = "/querySelectResource")
    public JSONMessage querySelectResource(){
        List<ResourceInfo> resourceInfos = resourceInfoManager.queryResourceInfo();
        List<ResultDataSelect> data = new ArrayList<>();
        for (ResourceInfo resourceInfo : resourceInfos) {
            ResultDataSelect resultDataSelect = new ResultDataSelect();
            resultDataSelect.setName(resourceInfo.getResourceName());
            resultDataSelect.setValue(String.valueOf(resourceInfo.getId()));
            data.add(resultDataSelect);
        }
        return JSONMessage.success(data);
    }

    /**
     * @Description //TODO 查看全部资源
     * @Date 11:24 2020/3/9
     **/
    @RequestMapping(value = "/querySelectAllResource")
    public JSONMessage querySelectAllResource(){
        //查出全部资源
        List<ResourceInfo> resourceInfos = resourceInfoManager.queryResourceInfo();
        /*for (ResourceInfo resourceInfo : resourceInfos) {
            resourceInfo.setAa(String.valueOf(resourceInfo.getId()));
            resourceInfo.setId(null);
        }
        System.out.println(JSONObject.toJSON(resourceInfos));*/
        List<ResultDataSelect2> data = new ArrayList<>();

        //先将最高的父节点给遍历出来
        for (ResourceInfo resourceInfo : resourceInfos) {
            if (resourceInfo.getPid().equals("0") || resourceInfo.getPid().equals("")){
                if (resourceInfo.getStatus() == 1){//判读是否禁用
                    ResultDataSelect2 resultDataSelect2 = new ResultDataSelect2();
                    resultDataSelect2.setId(String.valueOf(resourceInfo.getId()));
                    resultDataSelect2.setResourceName(resourceInfo.getResourceName());
                    resultDataSelect2.setResourceAuth(resourceInfo.getResourceAuth());
                    resultDataSelect2.setResourceUrl(resourceInfo.getResourceUrl());
                    resultDataSelect2.setIsView(resourceInfo.getIsView());
                    resultDataSelect2.setStatus(resourceInfo.getStatus());
                    resultDataSelect2.setType(resourceInfo.getType());
                    data.add(resultDataSelect2);
                }
            }
        }

        //遍历刚刚数据的最高节点
        for (ResultDataSelect2 datum : data) {
            //根据Pid看看是否有数据
            List<ResourceInfo> resourceInfos1 = resourceInfoManager.queryResourceInfoByPid(String.valueOf(datum.getId()));
            List<ResultDataSelect2> list = new ArrayList<>();
            if (resourceInfos1.size() > 0){
                //将遍历出来的二级节点加入
                for (ResourceInfo resourceInfo : resourceInfos1) {
                    if (resourceInfo.getStatus() == 1){
                        ResultDataSelect2 resultDataSelect2 = new ResultDataSelect2();
                        resultDataSelect2.setResourceName(resourceInfo.getResourceName());
                        resultDataSelect2.setResourceAuth(resourceInfo.getResourceAuth());
                        resultDataSelect2.setResourceUrl(resourceInfo.getResourceUrl());
                        resultDataSelect2.setStatus(resourceInfo.getStatus());
                        resultDataSelect2.setIsView(resourceInfo.getIsView());
                        resultDataSelect2.setType(resourceInfo.getType());
                        resultDataSelect2.setId(String.valueOf(resourceInfo.getId()));
                        List<ResultDataSelect2> recursion = recursion(resourceInfo);
                        resultDataSelect2.setChildren(recursion);
                        list.add(resultDataSelect2);
                    }
                }
            }
            datum.setChildren(list);
        }

        return JSONMessage.success(data);
    }




    private List<ResultDataSelect2> recursion(ResourceInfo resourceInfo){
        List<ResultDataSelect2> resultDataSelect2s = new ArrayList<>();
        List<ResourceInfo> resourceInfos1 = resourceInfoManager.queryResourceInfoByPid(String.valueOf(resourceInfo.getId()));
        if (resourceInfos1.size() > 0){
            for (ResourceInfo info : resourceInfos1) {
                if (info.getStatus() == 1){
                    ResultDataSelect2 resultDataSelect2 = new ResultDataSelect2();
                    resultDataSelect2.setId(String.valueOf(info.getId()));
                    resultDataSelect2.setResourceName(info.getResourceName());
                    resultDataSelect2.setResourceAuth(info.getResourceAuth());
                    resultDataSelect2.setResourceUrl(info.getResourceUrl());
                    resultDataSelect2.setIsView(info.getIsView());
                    resultDataSelect2.setStatus(info.getStatus());
                    resultDataSelect2.setType(info.getType());
                    List<ResultDataSelect2> recursion = recursion(info);
                    resultDataSelect2.setChildren(recursion);
                    resultDataSelect2s.add(resultDataSelect2);
                }
            }
            return resultDataSelect2s;
        }else{
            return resultDataSelect2s;
        }
    }


}
